package com.css.dzj.dzjportal.modules.message.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjportal.common.util.Constant;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.modules.message.entity.PortalMsgEntity;
import com.css.dzj.dzjportal.modules.message.service.MessageService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 信息controller
 *
 * @author zhouchanghe
 */
@PropertySource(value = "classpath:httpurl.properties")
@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    //外国人来华系统url
    @Value("${WGR_URL}")
    private  String wgrRestUrl;
    /**
     * 查询消息列表
     *
     * @param
     * @return
     * @author zhouchanghe
     * @description TODO
     * @date
     **/

    @GetMapping("/findPage")
    public R getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                         @RequestParam(name = "complainTopic", required = false) String complainTopic,
                         HttpServletRequest request) {
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        QueryResults<PortalMsgEntity> portalMsgEntity = messageService.getPage(complainTopic,pageRequest,request);
        return R.ok().put("data", portalMsgEntity);
    }

    /**
     * 保存消息
     * @param portalMsgEntity
     * @return
     */
    @PostMapping("/save")
    public R saveMessage(@RequestBody PortalMsgEntity portalMsgEntity) {
        try {
            messageService.saveDB(portalMsgEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        //baseService.logSuccess(3,"添加成功","2");
        //return R.ok();
    }
    /**
     * 修改消息
     * @param id
     * @return
     */
    @PostMapping("/update")
    public R updateMessage(@RequestParam String id) {
        try {
            PortalMsgEntity portalMsgEntity = messageService.findById(id);
            //打开消息详情，默认已阅
            portalMsgEntity.setStatus(Constant.MSG_YY);
            messageService.update(portalMsgEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }
    /**
     * 通过关联id获取投诉举报信息
     * @param linkId
     * @return
     */
    @GetMapping("/getMessage")
    public R getMessage(@RequestParam String linkId,@RequestParam String id) {
        String msg = messageService.findByLinkId(linkId,id,wgrRestUrl);
        JSONObject jsonObject=(JSONObject) JSON.parse(msg);
        if(null!= jsonObject && jsonObject.get("code").equals(200)){
            return R.ok().put("data",jsonObject.get("data"));

        }else {
            return R.error("查询失败");
        }

    }
    /**
     * 通过当前登录人查询消息个数(未阅读的消息)
     * @param
     * @return
     */
    @GetMapping("/getMessageCount")
    public R getMessageCount(HttpServletRequest request) {
        String count = messageService.getCountMsg(request);
        //noSessionMessage只是标识位，count为noSessionMessage时表示没有用户登陆，session不存在，不需要写日志
        if(count.equals("noSessionMessage")){
            count = "0";
        }else{
           // baseService.logSuccess(3,"查询成功","2");
        }
        return R.ok().put("data", count);
    }
}
